package net.sansi.v3correctserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.R;
import net.sansi.v3correctserver.dto.analyse.PhotoAnalyseDto;
import net.sansi.v3correctserver.entity.IntPoint;
import net.sansi.v3correctserver.entity.analyse.PhotoAnalyseResult;
import net.sansi.v3correctserver.entity.image.RecImage;
import net.sansi.v3correctserver.entity.screen.CorrectArea;
import net.sansi.v3correctserver.service.analyse.AnalyseService;
import net.sansi.v3correctserver.service.photorec.rec.RecFence;
import net.sansi.v3correctserver.task.AutoAnalysePhotoTask;
import net.sansi.v3correctserver.task.V3Task;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.nio.file.Path;
import java.util.List;

/**
 * Created by Fang
 * create time : 2024/11/1
 * IDE : IntelliJ IDEA
 */

@Api(tags = "相片分析接口")
@RestController
@RequestMapping("/analyse")
public class AnalyseController {

    @Resource
    private AnalyseService analyseService;

    /**
     * 相片分析
     *
     * @param form 请求参数
     * @return 相片分析结果
     */
    @ApiOperation("相片分析")
    @PostMapping("/rect/led/photo")
    public R<PhotoAnalyseResult.Rect> rectPhoto(@RequestBody @Validated PhotoAnalyseDto.RectPhotoReq form) {
        return R.data(analyseService.rectPhoto(form));
    }

    /**
     * 拍摄分析(需要相机参数和分区)
     *
     * @param form 请求参数
     * @return 相片分析结果
     */
    @ApiOperation("拍摄分析(需要相机参数和分区)")
    @PostMapping("/rect/led/take")
    public R<PhotoAnalyseResult.Rect> rectTake(@RequestBody @Validated PhotoAnalyseDto.RectTakeReq form) {
        return R.data(analyseService.rectTake(form));
    }

    /**
     * 自动分析
     *
     * @param form 请求参数
     * @return 任务ID
     */
    @ApiOperation("自动分析")
    @PostMapping("/rect/led/auto")
    public R<String> rectAuto(@RequestBody @Validated PhotoAnalyseDto.RectAutoReq form) {
        return R.data(analyseService.startAutoAnalysePhoto(form));
    }

    /**
     * 自动分析进度查询
     *
     * @param taskId 任务ID
     * @return 自动分析进度
     */
    @ApiOperation("自动分析进度查询")
    @GetMapping("/rect/led/auto/progress")
    public R<AutoAnalysePhotoTask.Progress> rectAutoProgress(@RequestParam("taskId") @NotBlank() String taskId) {
        V3Task<?, ?> task = AppHelper.getTaskManager().get(taskId);
        return R.data((AutoAnalysePhotoTask.Progress) task.getProgress());
    }

    /**
     * 矩形屏围栏识别（客户端拍摄上传）
     *
     * @param form 请求参数
     * @return 坐标集合[左上，右上，右下，左下]
     */
    @ApiOperation("矩形屏围栏识别（客户端拍摄上传）")
    @PostMapping("/rect/fence/photo")
    public R<List<IntPoint>> rectFencePhoto(@RequestBody @Validated PhotoAnalyseDto.RectFenceReq form) {
        RecFence recFence = new RecFence(
                RecImage.load(Path.of(form.getLedPath())),
                form.getBlackPath() == null ? null : RecImage.load(Path.of(form.getBlackPath()))
        );
        return R.data(recFence.searchFence());
    }

    /**
     * 矩形屏围栏识别（后端自行拍摄）
     *
     * @param form 请求参数
     * @return 围栏识别结果
     */
    @ApiOperation("矩形屏围栏识别（后端自行拍摄）")
    @PostMapping("/rect/fence/take")
    public R<PhotoAnalyseDto.RectFenceResp> rectFenceTake(@RequestBody @Validated PhotoAnalyseDto.RectTakeReq form) {
        return R.data(analyseService.rectFenceTake(form));
    }

    @ApiOperation("自动计算分区")
    @PostMapping("/rect/auto-split-area")
    public R<List<CorrectArea>> rectAutoSplitArea(@RequestBody @Validated PhotoAnalyseDto.RectAutoSplitAreaReq form) {
        return R.data(analyseService.autoComputeArea(form));
    }
}
